package com.qf.fmall2202.utils;

import cn.hutool.http.HttpUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpTest {
    public static void main(String[] args) throws IOException {

        //1. 如何通过java代码访问 百度呢 ？

        // 创建URL对象
        String urlStr = "https://www.baidu.com";
        final URL url = new URL(urlStr);

        // 建立到url的连接
        final URLConnection urlConnection = url.openConnection(); // 注意，这里并没有真正发送http请求，只是创建了URLConnection对象

        urlConnection.connect(); // 真正发送http请求

        final InputStream inputStream = urlConnection.getInputStream();

        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line = null;

        while ( (line = bufferedReader.readLine()) !=null){
            System.out.println(line);
        }

        bufferedReader.close();
        inputStream.close();


    }
}
